import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadDemo14 {
    //生产者消费者模型
    public static void main(String[] args) {
        //java库中提供的阻塞队列，线程安全
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
        //生产者模型
        Thread producer = new Thread(() -> {
            int value = 0;
            while(true) {
                try {
                    Thread.sleep(1000);
                    System.out.println("生产" + value);
                    blockingQueue.put(value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                value++;
            }
        });
        //消费者模型
        Thread consumer = new Thread(() -> {
            int value = 0;
            while(true) {
                try {
                    value = blockingQueue.take();
                    System.out.println("消费" + value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        producer.start();
        consumer.start();

    }
}
